<?php

namespace Home\Controller;

/**
* 
*/
class CollectController extends HomeController
{
	/**
	 * 收藏
	 * @author qlj
	 */
	public function add(){		
		if (IS_AJAX) {
			$_POST['uid'] = session('uid');
			//查找是否已收藏
			if (D('Collect') -> collectList($_POST['uid'], $_POST['c_uid'], $_POST['teaching'])) {
				$this -> ajaxReturn('已经收藏过了');
			} else {
				if (D('Collect') -> add($_POST)) {
					$this -> ajaxReturn('收藏成功！');
				} else {
					$this -> ajaxReturn('出错了');
				}		
			}
		}
	}

	/**
	 * 收藏列表
	 * @param  int $teaching [教or学]
	 * @author qlj
	 */
	public function collectList($teaching=''){
		if (IS_POST) {
			// dump($_POST);die;
			//删除收藏记录
			if (D('Collect') -> delete(implode(',', $_POST['ids']))) {
				$this -> success('删除成功');
			}
			$this -> error('未选中用户');
		} else {
			if ($teaching == '') {
				$teaching = $_GET['teaching'] = 2;
			}
			$collectList = D('Collect') -> collectList(session('uid'),$c_uid='',$teaching);

			foreach ($collectList as &$v) {
				//查出课程名称
				$courses = D('Course') -> userCourses($v['c_uid'],$teaching);
				foreach ($courses as $val) {
					$v['courses'][] = $val['title'];
				}
				if(count($v['courses'])>3) {
					$v['courses'] = array_slice($v['courses'],0,3);
				}
				//被收藏人发布课程的最低价格
				$v = array_merge($v,D('Course') -> minSalary($v['c_uid'],$teaching));
			}
			$this -> assign('collectList',$collectList);
			$this -> display();	
		}
				
	}
}